package com.example.glide;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.glide.options.GlideApp;

import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {

    private ImageView iv_pic;
    private ImageView iv_submit;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv_pic = findViewById(R.id.iv_pic);
        iv_submit = findViewById(R.id.iv_submit);

        //TODO 正常使用Glide加载图片
        //设置请求图片的参数
        // TODO  RequestOptions options = new RequestOptions().centerCrop().override(144);
        // TODO  Glide.with(this)
        // TODO    .load("https://img.073980.com/2022/09/222.png")
        //添加请求图片的参数
        // TODO    .apply(options)
        // TODO    .into(iv_pic);

        //TODO 通过Glide自动创建的对象进行加载图片 将请求图片的参数放入自定义api当中
        GlideApp.with(this)
                //TODO 可以用as进行图片的类型转换
                //.asBitmap()
                //.asDrawable()
                //.asFile()
                .load("https://img.073980.com/2022/09/222.png")
                .applyAvatar(144 * 4)
                //图片的淡入淡出效果
                .transition(DrawableTransitionOptions.withCrossFade())
                //对图片进行监听
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(iv_pic);

        //TODO 通过submit的方式得到图片资源 再进行展示 主要是图片资源的获取
        FutureTarget<Drawable> target = GlideApp.with(this)
                .asDrawable()
                .load("https://pic1.zhimg.com/50/v2-26717b91589b6df36b365c9c5211dfbe_720w.jpg?source=1940ef5c")
                .applyAvatar(144 * 4)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                })
                .transition(DrawableTransitionOptions.withCrossFade())
                .submit();
        //TODO 通过target得到draw对象 得到对象的过程是一个同步过程 需要在子线程中进行
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Drawable drawable = target.get();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            iv_submit.setImageDrawable(drawable);
                        }
                    });
                } catch (ExecutionException e) {
                    throw new RuntimeException(e);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }
}